/**
 * Candidate number: 35834
 */
package tv.starfu.animae;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.World;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import tv.starfu.components.Transform;
import tv.starfu.components.level.GridPosition;
import tv.starfu.components.level.KyberneteGrid;
import tv.starfu.components.level.Path;


public class Tile extends Anima {

    private Transform transform;
    private GridPosition gridPos;
    private Path path;
    private KyberneteGrid kyberGrid;
    
    private Geometry tile;
    private Node systemRoot = new Node("systemRoot");
    private Node root;
    private Material material1;
    private Material material2;
    private Texture texture1;
    private Texture texture2;
   
    
    private SimpleApplication app;
    // + anything else needed to render player, eg OpenGL, pics etc
    
    
    public Tile(World world, Entity owner, SimpleApplication app, Node rootNode) {
        super(world, owner, app);
        this.app = app;
        this.root = rootNode;
    }
    
    @Override
    public void initalize() {
        System.out.println("Tile/Anima : initialising");
        
        ComponentMapper<Transform> transformMapper      = new ComponentMapper<Transform>(Transform.class, world);
        ComponentMapper<GridPosition> gridPosMapper     = new ComponentMapper<GridPosition>(GridPosition.class, world);
        ComponentMapper<Path> pathMapper                = new ComponentMapper<Path>(Path.class, world);
        ComponentMapper<KyberneteGrid> kyberGridMapper  = new ComponentMapper<KyberneteGrid>(KyberneteGrid.class, world);
        
        path        = pathMapper.get(owner);
        gridPos     = gridPosMapper.get(owner);
        transform   = transformMapper.get(owner);
        kyberGrid   = kyberGridMapper.get(owner);
        
        int x = gridPos.x;
        int y = gridPos.y;
      
          // Get position of KyberGrid
            Vector3f parentGridPos = new Vector3f(0f,0f,0f);
            
            int gridWidth         = kyberGrid.width;
            int gridLength        = kyberGrid.length;
            
            // Go to North West corner
            parentGridPos.z -= (10*0.5f*gridLength);
            parentGridPos.x -= (10*0.5f*gridWidth);
            
            // Set Tile Transform, hopefully!!
           // transform.position = 
             //       parentGridPos.add(new Vector3f((x+1)*10f, 0f, 
               //                              (y+1)*10f));        
        
        AssetManager assetManager = app.getAssetManager();
        
        Box box = new Box(3f,0.1f,3f);
        tile = new Geometry("Box", box);        
        
        material2 = new Material(assetManager, 
                "Common/MatDefs/Misc/Unshaded.j3md");
        texture2 = assetManager.loadTexture("Textures/astrosky.jpg");
        material2.setTexture("ColorMap", texture2);
        tile.setMaterial(material2); 
        
       
        
//        tile.setLocalTranslation(transform.position); 

        systemRoot.attachChild(tile);
        System.out.print(" ............made tile planes");
        root.attachChild(systemRoot);
    }

    @Override
    public void render() {
            
    }
    
}
